package com.luojialong.onlineJudge.judge;

import cn.hutool.json.JSONUtil;
import com.luojialong.onlineJudge.common.ErrorCode;
import com.luojialong.onlineJudge.exception.BusinessException;
import com.luojialong.onlineJudge.judge.codesandbox.CodeSandBox;
import com.luojialong.onlineJudge.judge.codesandbox.CodeSandboxProxy;
import com.luojialong.onlineJudge.judge.codesandbox.impl.RemoteCodeSandbox;
import com.luojialong.onlineJudge.judge.codesandbox.model.ExecuteCodeRequest;
import com.luojialong.onlineJudge.judge.codesandbox.model.ExecuteCodeResponse;
import com.luojialong.onlineJudge.judge.strategy.DefaultJudgeStrategy;
import com.luojialong.onlineJudge.judge.strategy.JudgeContext;
import com.luojialong.onlineJudge.model.dto.question.JudgeCase;
import com.luojialong.onlineJudge.judge.codesandbox.model.JudgeInfo;
import com.luojialong.onlineJudge.model.entity.Question;
import com.luojialong.onlineJudge.model.entity.QuestionSubmit;
import com.luojialong.onlineJudge.model.enums.JudgeInfoMessageEnum;
import com.luojialong.onlineJudge.model.enums.QuestionSubmitStatusEnum;
import com.luojialong.onlineJudge.service.QuestionService;
import com.luojialong.onlineJudge.service.QuestionSubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class JudgeServiceImpl implements JudgeService {

    @Autowired
    private QuestionSubmitService questionSubmitService;

    @Autowired
    private QuestionService questionService;


    @Override
    @RequestMapping
    public QuestionSubmit doJudge(long questionSubmitId) {
        QuestionSubmit questionSubmit = questionSubmitService.getById(questionSubmitId);
        if (questionSubmit == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "提交信息不存在");
        }
        Long questionId = questionSubmit.getQuestionId();
        Question question = questionService.getById(questionId);
        if (question == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "题目不存在");
        }
        if (!questionSubmit.getStatus().equals(QuestionSubmitStatusEnum.WARNING.getValue())) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "题目正在判题中");
        }
        QuestionSubmit questionSubmitUpdate = new QuestionSubmit();
        questionSubmitUpdate.setId(questionSubmitId);
        questionSubmitUpdate.setStatus(QuestionSubmitStatusEnum.RUNNING.getValue());
        boolean update = questionSubmitService.updateById(questionSubmitUpdate);
        if (!update) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "题目状态更新错误");
        }
        CodeSandBox codeSandBox = new RemoteCodeSandbox();
        codeSandBox = new CodeSandboxProxy(codeSandBox);
        String language = questionSubmit.getLanguage();
        String code = questionSubmit.getCode();
        String judgeCaseStr = question.getJudgeCase();
        List<JudgeCase> judgeCaseList = JSONUtil.toList(judgeCaseStr, JudgeCase.class);
        List<String> inputlist = judgeCaseList.stream().map(JudgeCase::getInput).collect(Collectors.toList());
        ExecuteCodeRequest executeCodeRequest = ExecuteCodeRequest.builder()
                .code(code)
                .language(language)
                .inputList(inputlist)
                .build();
        ExecuteCodeResponse executeCodeResponse = codeSandBox.executeCode(executeCodeRequest);
        List<String> ouputList = executeCodeResponse.getOuputList();

        JudgeContext judgeContext = new JudgeContext();
        System.out.println("--------------------------------");
        System.out.println("题目沙箱的信息");
        System.out.println(executeCodeResponse.getJudgeInfo());
        System.out.println("--------------------------------");
        judgeContext.setJudgeInfo(executeCodeResponse.getJudgeInfo());
        judgeContext.setInputList(inputlist);
        judgeContext.setOutputList(ouputList);
        judgeContext.setJudgeCaseList(judgeCaseList);
        judgeContext.setQuestion(question);
        judgeContext.setQuestionSubmit(questionSubmit);
        JudgeInfo judgeInfo = new DefaultJudgeStrategy().doJudge(judgeContext, executeCodeResponse);
        questionSubmitUpdate = new QuestionSubmit();
        questionSubmitUpdate.setId(questionSubmitId);
        questionSubmitUpdate.setCodeStatus(questionSubmit.getCodeStatus());
        if (judgeInfo != null) {
            questionSubmitUpdate.setJudgeInfo(JSONUtil.toJsonStr(judgeInfo));
        }
        if (!questionSubmit.getCodeStatus().equals(JudgeInfoMessageEnum.ACCEPTED.getText())) {
            questionSubmitUpdate.setStatus(QuestionSubmitStatusEnum.FAILED.getValue());
        } else {
            questionSubmitUpdate.setStatus(QuestionSubmitStatusEnum.SUCCEED.getValue());
        }
        if (questionSubmit.getCodeStatus().equals(JudgeInfoMessageEnum.ACCEPTED.getText())) {
            question.setAcceptedNum(question.getAcceptedNum() == null ? 1 : question.getAcceptedNum() + 1);
            questionSubmitService.updateIntegral(questionSubmit.getUserId(), question);
        }
        update = questionSubmitService.updateById(questionSubmitUpdate);
        boolean b = questionService.updateById(question);
        if (!update) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "题目状态更新错误");
        }
        QuestionSubmit questionSubmitResult = questionSubmitService.getById(questionSubmitId);
        return questionSubmitResult;
    }
}
